package com.example.messages.utils;

import com.example.messages.entity.AppointmentInfo;
import com.github.qcloudsms.SmsSingleSender;
import com.github.qcloudsms.SmsSingleSenderResult;
import com.github.qcloudsms.httpclient.HTTPException;
import org.json.JSONException;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

@Component
public class SendMessage {
    static int appid = 1400666225;
    static String appkey = "dd8aff75e50c5d0682bc66950e3267a5";
    static String signature = "小项目站个人网";
    static String time = "5";

    //产生4位数验证码
    private static String getCode(){
        Random r = new Random();
        String code = Integer.toString(r.nextInt(10000));
        while (code.length() < 4){
            code = code + "0";
        }
        return code;
    }

    //发送验证码
    public static String sendCode(String phone){
        try {
            Date date = new Date();
            SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
            System.out.println();
            int templateid = 1372769;
            String code = getCode();
            String [] params = new String[]{code, time};
            SmsSingleSender  smsSingleSender = new SmsSingleSender(appid,appkey);
            SmsSingleSenderResult result = smsSingleSender.sendWithParam("86",phone,templateid,params,signature,"","");
            System.out.println("当前时间: " + formatter.format(date) + " 结果码：" + result.result + " 结果信息：" + result.errMsg);
            if (result.errMsg.equals("OK")) return code;
            else return "Exception";
        }
        catch (HTTPException | JSONException | IOException e){
            e.printStackTrace();
            return "Exception";
        }
    }

    //发送对应通知信息
    public static String sendResultMessage(String phone){
        try {
            Date date = new Date();
            SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
            int templateid  = 1411372;//结果上传成功
            String [] params = new String[]{};
            SmsSingleSender  smsSingleSender = new SmsSingleSender(appid,appkey);
            SmsSingleSenderResult result = smsSingleSender.sendWithParam("86",phone,templateid,params,signature,"","");
            System.out.println("当前时间: " + formatter.format(date) + " 结果码：" + result.result + " 结果信息：" + result.errMsg);
            if (result.errMsg.equals("OK")) return "结果通知短信发送成功";
            else return "结果通知短信发送失败";
        }
        catch (HTTPException | JSONException | IOException e){
            e.printStackTrace();
            return "结果通知短信发送失败";
        }
    }
    public static String sendAppointmentMessage(AppointmentInfo appointmentInfo){
        try {
            Date date = new Date();
            SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
            int templateid  = 1411302;//预约成功
            String phone = appointmentInfo.getPhone();
            String time = appointmentInfo.getTime();
            String[] times = time.split(" ");
            String address = appointmentInfo.getAddress();
            String address1 = "";
            String address2 = "";
            if (address.length() <= 12)
            {
                address1 = address;
            }
            else if (address.length() <=24){
                address1 = address.substring(0,12);
                address2 = address.substring(12);
            }
//            System.out.println("times1:=" + times[0] + "  长度为：" + times[0].length());
//            System.out.println("times2:=" + times[1] + "  长度为：" + times[1].length());
//            System.out.println("address1:=" + address1 + "  长度为：" + address1.length());
//            System.out.println("address2:=" + address2 + "  长度为：" + address2.length());
            String [] params = new String[]{
                    String.valueOf(appointmentInfo.getId()),
                    times[0],
                    times[1],
                    address1,
                    address2
                    };
            SmsSingleSender  smsSingleSender = new SmsSingleSender(appid,appkey);
            SmsSingleSenderResult result = smsSingleSender.sendWithParam("86",phone,templateid,params,signature,"","");
            System.out.println("当前时间: " + formatter.format(date) + " 结果码：" + result.result + " 结果信息：" + result.errMsg);
            if (result.errMsg.equals("OK")) return "预约通知短信发送成功";
            else return  "预约通知短信发送失败";
        }
        catch (HTTPException | JSONException | IOException e){
            e.printStackTrace();
            return  "预约通知短信发送失败";
        }
    }
}
